/*
 * ModQuit.java
 *
 * Created on 21 de agosto de 2007, 04:25 PM
 *
 * Modulo encargado de asegurar salida limpia de Sistema.
 *
 * <p>Company: Soluciones Info Moviles S.A.C.</p>
 * @version 1.0.0
 */

package sim.framework;
import sim.framework.dialog.*;

public class ModQuit extends Module {
    
    /** Creates a new instance of ModQuit */
    public ModQuit(int i, Simlet s) {
        super(i, s);
    }
    
    public void work(final Msg msg) {
        
        //#ifdef DEBUG_MODQUIT
        //# System.out.println("ModQuit::work() ENTERED");
        //#endif
        
        switch(m_modID) {
            case Simlet.MOD_QUIT:
                moduleQuit(msg);
                break;
            case Simlet.MOD_KILL:
                moduleKill(msg);
                break;
            case Simlet.MOD_FAIL:
                moduleFail(msg);
                break;
        }
        
        switch (msg.getWParam()) {
            case Executable.STATE_INIT:
                break;
        }
        
        //#ifdef DEBUG_MODQUIT
        //# System.out.println("ModQuit::work() EXITING");
        //#endif
    }
    
    public boolean event(final Msg msg) {
        boolean ret = false;

        switch( msg.getEventCode() ) {
            case EventListener.COMMAND:
                switch (msg.getWParam()) {
                    case Dialog.CTRL_BUTTON_NEXT:
                        moduleKill(msg);
                        ret = true;
                        break;
                    case Dialog.CTRL_BUTTON_BACK:
                        execBack();
                        ret = true;
                        break;
                }
        }
        
        return ret;
    }
    
    private void moduleQuit(final Msg msg) {
        DlgWarning dlg;
        dlg = (DlgWarning)DlgWarning.create(m_Simlet,
                getLabel(Resource.LBL_TITLE_APP),
                getLabel(Resource.LBL_MESG_QUIT_QUESTION),
                getLabel(Resource.LBL_CTRL_YES),
                getLabel(Resource.LBL_CTRL_NO));

        setGui(dlg);
        
        // Select focus on Softmenu's item "NO"
        dlg.setFocus(Dialog.CTRL_SOFTM);
        dlg.selectItem(Dialog.CTRL_SOFTM, Dialog.CTRL_BUTTON_BACK);
    }
    
    private void moduleKill(final Msg msg) {
        m_Simlet.destroyApp(true);
        m_Simlet.notifyDestroyed();
    }
    
    private void moduleFail(final Msg msg) {
        DlgNotify dlg;
        dlg = (DlgNotify)DlgNotify.create(m_Simlet,
                getLabel(Resource.LBL_TITLE_APP),
                m_Simlet.m_QuitMessage,
                getLabel(Resource.LBL_CTRL_ACCEPT),
                null);
        
        setGui(dlg);
    }
}
